﻿using ASmile.Com.Log;
using ASmile.DBModel;
using ASmile.Extend;
using ASmile.ORM;
using ASmile.ORM.Cores;
using ASmile.Tools;
using System;

namespace ASmile.Com.Da
{
    public class LogBLL : BLLBase, ILogBLL
    {
        public LogBLL()
            : base(ConfigCom.M.LogDbConnStr)
        { }

        public ApiResult<MiniTable> GetLogTable(ArgLogBase arg)
        {
            MiniTable rTable;
            if (arg.LogFormName == LogFormName.App)
            {
                rTable = GetAppTable(arg);
            }
            else if (arg.LogFormName == LogFormName.HTTP)
            {
                rTable = GetHttpLogTable(arg);
            }
            else if (arg.LogFormName == LogFormName.LogSql)
            {
                rTable = GetSqlLogTable(arg);
            }
            else rTable = null;

            return RetOK(rTable);
        }

        MiniTable GetLogPager<TEntity>(ArgLogBase arg, Action<QueryCondition<TEntity>> QueryHandle)
            where TEntity : class, IDBLog, new()
        {
            return
            GetPagerTable<TEntity>(arg, (query) =>
            {
                query.WhereAnd(s => s.LogTime >= arg.StartTime);
                if (arg.EndTime != null)
                {
                    query.WhereAnd(s => s.LogTime <= arg.EndTime.Value);
                }
                QueryHandle(query);
                query.OrderBy(s => s.LogTime, EmQueryOrder.Desc);
            });
        }

        /// <summary>
        /// 获取应用日志（Log4Net）
        /// </summary>
        MiniTable GetAppTable(ArgLogBase arg)
        {
            return
            GetLogPager<VLog_App>(arg, (query) =>
            {
                if (arg.SearchVal.IsNotEmpty())
                {
                    string likeStr = $"%{arg.SearchVal}%";
                    query.WhereAnd(s => s.WEx_Like(s.Message, likeStr));
                }
            });
        }
        /// <summary>
        /// 获取Http日志
        /// </summary>
        MiniTable GetHttpLogTable(ArgLogBase arg)
        {
            return
            GetLogPager<VLog_Http>(arg, (query) =>
            {
                if (arg.SearchVal.IsNotEmpty())
                {
                    string likeStr = $"%{arg.SearchVal}%";
                    query.WhereAnd(s => s.WEx_Like(s.Path, likeStr));
                }
            });
        }
        /// <summary>
        /// 获取 sql 请求日志
        /// </summary>
        MiniTable GetSqlLogTable(ArgLogBase arg)
        {
            return
            GetLogPager<VLog_Sql>(arg, (query) =>
            {
                if (arg.SearchVal.IsNotEmpty())
                {
                    string likeStr = $"%{arg.SearchVal}%";
                    query.WhereAnd(s =>
                        s.WEx_Like(s.ResultMark, likeStr) ||
                        s.WEx_Like(s.TableName, likeStr));
                }
            });
        }

        public ApiResult<Log_App> GetAppDetail(int id)
        {
            var r = DB.Query<Log_App>(s => s.Id == id).ToEntity();
            return RetOK(r, "OK");
        }

        public ApiResult<Log_Http> GetHttpDetail(int id)
        {
            var r = DB.Query<Log_Http>(s => s.Id == id).ToEntity();
            return RetOK(r, "OK");
        }

        public ApiResult<Log_Sql> GetSqlDetail(int id)
        {
            var r = DB.Query<Log_Sql>(s => s.Id == id).ToEntity();
            return RetOK(r, "OK");
        }
    }
}